import Vue from 'vue'
import Router from 'vue-router'
import Cookies from 'js-cookie'
import Store from 'store'

// 路由数据
import routes from './routes'
import { Message } from 'element-ui';
Vue.use(Router)

let router = new Router({ routes })

/**
 * 路由拦截
 * 权限验证
 */
router.beforeEach((to, from, next) => {
  if(!Cookies.get('x-token') && to.path != '/login'){
    next({
      name: 'login'
    })
  } else if(to.meta.requiresAuth) {
    if(Store.get('x-user').userName == 'superAdmin') {
      next()
    } else {
      Message.error('您没有权限访问该页面')
      next(from.path)
    }
  }else {
    next ()
  }
})

router.afterEach(to => {
  if(to.meta.title){
    document.title = to.meta.title
  }
  if(document.getElementsByClassName('content')[0]){
    document.getElementsByClassName('content')[0].scrollTo(0,0);
  }
})

export default router